﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vaffanculo.Data.Interfaces;
using Vaffanculo.Data.Implements;
using System.Transactions;

namespace Vaffanculo.Business.Tests
{
    public class BaseTestClass
    {
        public void ExecuteManager(Action<IUnitOfWork> coreMethod, Action postCommit=null)
        {
            // Object context definition to specific model/database
            DbContextVaffanculoTest contextVaffanculo = new DbContextVaffanculoTest();

            IDbContext context = new DbContextAdap(contextVaffanculo);

            IUnitOfWork uow = new UnitOfWork(context);
            bool success = false;

            try
            {
                coreMethod(uow);
                success = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occured. "
                        + "The operation cannot be retried."
                        + ex.Message);

            }

            if (success)
            {
                // Save all changes.
                uow.Commit();
                // Execute anonymous method after commit if exists
                if (postCommit != null)
                {
                    postCommit();
                }
            }
            else
            {
                Console.WriteLine("The operation could not be completed");
                
            }
            // Dispose connection
            uow.GetIDbContext().Dispose();
            /*
            using (TransactionScope transaction = new TransactionScope())
            {
                try
                {
                    coreMethod(uow);                    
                    transaction.Complete();
                    success = true;
                }
                catch (Exception ex)
                {                    
                    Console.WriteLine("An error occured. "
                            + "The operation cannot be retried."
                            + ex.Message);
                    
                }
            }//
            if (success)
            {
                // Save all changes.
                uow.Commit();
                if (postCommit != null)
                {
                    postCommit();
                }
            }
            else
            {
                Console.WriteLine("The operation could not be completed");
            }
            //uow.GetIDbContext().Dispose();
             * */
            
        }// End Method

    
    }// End Class
}
